/**
 * 
 */
package jnzb.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import jnzb.JNZB;
import jnzb.control.Controller;

/**
 * Contains the buttons that do things
 * 
 * @author Rid
 * 
 */
@SuppressWarnings("serial")
public class ControlPanel extends JPanel {

	protected static final String ABOUT_TEXT = "jNZB version "
			+ JNZB.VERSION + " - http://code.google.com/p/jnzb"
			+ "\nWritten by Richard Levy"
			+ "\nUses the Java YEnc Decoder - http://sourceforge.net/projects/jyenc/"
			+ "\n and Apache Commons - http://commons.apache.org";
	JButton addNZB = new JButton("Add NZB");
	JButton start = new JButton("Pause");
	JButton about = new JButton("About");
	JButton exit = new JButton("Exit");

	private JFileChooser fileChooser;
	private UserInterface userInterface;

	public ControlPanel(UserInterface userInterface) {
		this.userInterface = userInterface;
		create();
	}

	/**
	 * Create the layout
	 */
	private void create() {
		Border etchedBorder = BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED);
		Border titleBorder = BorderFactory.createTitledBorder(etchedBorder,
				"Control");
		this.setBorder(titleBorder);

		// this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		// this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		// Create a horizontal layout where theres some padding either side and
		// glue between the buttons so they won't expand but the glue will
		this.add(Box.createHorizontalStrut(10));
		this.add(addNZB);
		this.add(Box.createHorizontalGlue());
		this.add(start);
		this.add(Box.createHorizontalGlue());
		this.add(about);
		this.add(Box.createHorizontalGlue());
		this.add(exit);
		this.add(Box.createHorizontalStrut(10));

		int h = this.getPreferredSize().height;

		// Set max size to the preferred size so it'll only resize horizontally
		this.setMaximumSize(new Dimension(9999, h));

		fileChooser = new JFileChooser();
		fileChooser.addChoosableFileFilter(new nzbFilter());

	}

	public void registerListeners(final Controller controller) {
		this.addNZB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				int action = fileChooser.showOpenDialog(ControlPanel.this);
				if (action == JFileChooser.APPROVE_OPTION) {
					// Create code to launch the operation
					Runnable openNZB = new Runnable() {
						public void run() {
							controller.loadNZBEvent(fileChooser
									.getSelectedFile());
						}
					};

					// Wrap in a thread
					Thread workerThread = new Thread(openNZB);
					// Start the thread
					workerThread.start();

				}
			}
		});

		this.start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				// Create code to launch the operation
				Runnable toggle = new Runnable() {
					public void run() {
						controller.toggleStartEvent();
					}
				};

				// Wrap in a thread
				Thread workerThread = new Thread(toggle);
				// Start the threadtoggle
				workerThread.start();

			}
		});

		this.about.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				JOptionPane
						.showMessageDialog(userInterface, ABOUT_TEXT, "jNZB "
								+ JNZB.VERSION, JOptionPane.INFORMATION_MESSAGE);

			}
		});

		this.exit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				doExit(controller);
			}
		});
	}

	public void doExit(final Controller controller) {
		// If were not downloading just exit
		boolean exit = true;
		// If we're currently downloading, make sure we want to exit
		if (controller.isDownloading()) {
			int response = JOptionPane
					.showConfirmDialog(
							userInterface,
							"Downloads are still in progress. Are you sure you want to exit?",
							"Exit", JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE);
			exit = response == JOptionPane.YES_OPTION;
		}

		if (exit) {
			// Create code to launch the operation
			Runnable exitR = new Runnable() {
				public void run() {
					controller.exitEvent();
				}
			};
			// Wrap in a thread
			Thread workerThread = new Thread(exitR);
			// Start the threadtoggle
			workerThread.start();
		}

	}

	public void setPausedText(final boolean paused) {
		Runnable toggle = new Runnable() {
			public void run() {
				if (paused) {
					start.setText("Restart");
				} else {
					start.setText("Pause");
				}
			}
		};

		SwingUtilities.invokeLater(toggle);

	}
}
